package com.example.function.project.model.pojo;

import com.example.common.support.ApplicationContextProvider;
import com.example.function.pool.model.one.CarModel;
import com.example.function.pool.model.pojo.CarModelPojo;
import com.example.function.pool.service.ICarModelService;
import com.example.function.project.model.one.Project;
import com.example.function.project.model.one.ProjectFile;
import com.example.function.project.model.one.ProjectStage;
import com.example.function.project.model.one.ProjectUserRole;
import com.example.function.project.service.IProjectFileService;
import com.example.function.project.service.IProjectService;
import com.example.function.project.service.IProjectStageService;
import com.example.function.project.service.IProjectUserRoleService;
import com.example.function.sys.model.one.SysUser;
import com.example.function.sys.model.pojo.SysUserMiniAndProjectRolesPojo;
import com.example.function.sys.model.pojo.SysUserMiniPojo;
import com.example.function.sys.service.ISysUserService;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import org.springframework.beans.BeanUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * <h1>项目编辑对象</h1>
 * Created by hanqf on 2021/8/3 10:28.
 */


@Data
public class ProjectEditPojo implements Serializable {
    private static final long serialVersionUID = -6442900523204776225L;
    /*
     * 项目id
     */
    private Long id;

    /*
     * 项目代号，支持数字、字符及特殊字符，限制50字符
     */
    private String projectName;

    /*
     * 全功能开发完成时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd")
    private java.util.Date projectCompletionDate;


    /*
     * SOP截止时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd")
    private java.util.Date projectSopDate;


    /**
     * 创建人
     */
    private SysUserMiniPojo createUser;

    /**
     * 项目阶段
     */
    private ProjectStagePojo projectStage;

    /**
     * 车型列表
     */
    private List<CarModelPojo> carModels;

    /**
     * 项目文件
     */
    private List<ProjectFilePojo> files;


    /**
     * 项目用户
     */
    private List<SysUserMiniAndProjectRolesPojo> projectUsers;


    /**
     * 实体对象复制到数据对象
     */
    public static ProjectEditPojo wrapper(Project project) {
        ProjectEditPojo projectEditPojo = new ProjectEditPojo();
        if (project != null) {
            BeanUtils.copyProperties(project, projectEditPojo);
            final IProjectService projectService = (IProjectService) ApplicationContextProvider.getBean("projectService");

            //封装创建人
            final ISysUserService sysUserService = (ISysUserService) ApplicationContextProvider.getBean("sysUserService");
            final Long userId = project.getProjectUserId();
            final SysUser sysUser = sysUserService.findById(userId);
            projectEditPojo.setCreateUser(SysUserMiniPojo.wrapper(sysUser));

            //封项目阶段
            final IProjectStageService projectStageService = (IProjectStageService) ApplicationContextProvider.getBean("projectStageService");
            final ProjectStage projectStage = projectStageService.findById(project.getProjectStageId());
            projectEditPojo.setProjectStage(ProjectStagePojo.wrapper(projectStage));

            //封装车型列表
            final ICarModelService carModelService = (ICarModelService) ApplicationContextProvider.getBean("carModelService");
            final List<CarModel> carModels = carModelService.findCarModelsByProjectId(project.getId());
            projectEditPojo.setCarModels(CarModelPojo.wrapper(carModels));


            //封装项目文件
            final IProjectFileService projectFileService = (IProjectFileService) ApplicationContextProvider.getBean("projectFileService");
            final List<ProjectFile> files = projectFileService.findProjectFilesByProjectId(project.getId());
            projectEditPojo.setFiles(ProjectFilePojo.wrapper(files));


            //封装项目用户
            final IProjectUserRoleService projectUserRoleService = (IProjectUserRoleService) ApplicationContextProvider.getBean("projectUserRoleService");
            final List<ProjectUserRole> roles = projectUserRoleService.findProjectUserRolesByProjectId(project.getId());
            final List<SysUserMiniAndProjectRolesPojo> wrapper = SysUserMiniAndProjectRolesPojo.wrapper(roles);
            projectEditPojo.setProjectUsers(wrapper);


        }


        return projectEditPojo;
    }

    /**
     * 实体对象复制到数据对象List
     */
    public static List<ProjectEditPojo> wrapper(List<Project> projectList) {
        List<ProjectEditPojo> projectEditPojos = new ArrayList<>();
        for (Project project : projectList) {
            projectEditPojos.add(ProjectEditPojo.wrapper(project));
        }
        return projectEditPojos;
    }
}

